package com.otod.pm.doc.service;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import org.springframework.transaction.annotation.Transactional;

import com.otod.base.service.BaseService;
import com.otod.commons.utils.Collections3;
import com.otod.pm.doc.mapper.PmDocItemMapper;
import com.otod.pm.doc.vo.PmDocItemVo;
import com.otod.pm.doc.vo.PmDocMasterVo;

@Service("itemService")
@Transactional(readOnly=true)
public class PmDocItemService extends BaseService{
	protected static final Logger logger = LoggerFactory.getLogger(PmDocItemService.class);
	
	@Autowired
	private PmDocItemMapper docItemMapper;
	
	@Autowired
	private PmDocMasterService pmDocMasterService;
	
	@Transactional
	public void insert(PmDocItemVo pmDocItemVo){
		pmDocItemVo.setUuid(super.getUUID());
		docItemMapper.insert(pmDocItemVo);
	}
	
	@Transactional
	public void saveDocItem(PmDocMasterVo pmDocMasterVo,PmDocItemVo pmDocItemVo){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("stepId", pmDocMasterVo.getStepId());
		map.put("docName", pmDocMasterVo.getDocName());
		List<PmDocMasterVo> docMasterVoList = pmDocMasterService.queryForList(map);
		PmDocMasterVo masterVo = new PmDocMasterVo();
		if(Collections3.isNotEmpty(docMasterVoList)){
			masterVo = docMasterVoList.get(0);
		}else{
			masterVo = pmDocMasterService.insert(pmDocMasterVo);
		}
		
		map.clear();
		map.put("taskId", pmDocItemVo.getTaskId());
		List<PmDocItemVo> itemVoList = docItemMapper.queryForList(map);
		for (PmDocItemVo pmDocItemVo2 : itemVoList) {
			docItemMapper.deleteById(pmDocItemVo2.getUuid());
		}
		pmDocItemVo.setMasterId(masterVo.getUuid());
		this.insert(pmDocItemVo);
	}
	
	public List<PmDocItemVo> queryForList(Map<String,Object> map){
		return docItemMapper.queryForList(map);
		
	}
	
}
